<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | Bigotry <3162875@qq.com>                               |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\common\logic;

/**
 * 广告逻辑
 */
class Ad extends LogicBase
{
    
    /**
     * 广告分类编辑
     */
    public function adCatEdit($data = [])
    {
        
        $validate_result = $this->validateAdCat->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateAdCat->getError()];
        }
        
        $url = url('adCatList');
        
        $result = $this->modelAdCat->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '广告分类' . $handle_text . '，title：' . $data['title']);
        
        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelAdCat->getError()];
    }
    
    /**
     * 获取广告列表
     */
    public function getAdList($where = [], $field = 'a.*,c.title as category_name', $order = '')
    {
        
        $this->modelAd->alias('a');
        
        $join = [
					[SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                    [SYS_DB_PREFIX . 'ad_cat c', 'a.catid = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelAd->join = $join;
        
        return $this->modelAd->getList($where, $field, $order);
    }
    
    /**
     * 获取广告列表搜索条件
     */
    public function getWhere($data = [])
    {
        
        $where = [];
        isset($data['catid']) && $where['a.catid']=$data['catid'];
        !empty($data['search_data']) && $where['a.title|c.title'] = ['like', '%'.$data['search_data'].'%'];
        
        return $where;
    }
    
    /**
     * 广告信息编辑
     */
    public function adEdit($data = [])
    {
        
        $validate_result = $this->validateAd->scene('edit')->check($data);
        
        if (!$validate_result) {
            
            return [RESULT_ERROR, $this->validateAd->getError()];
        }
        
        $url = url('adList');
        
        empty($data['id']) && $data['member_id'] = MEMBER_ID;
        
       // $data['content'] = html_entity_decode($data['content']);
        
        $result = $this->modelAd->setInfo($data);
        
        $handle_text = empty($data['id']) ? '新增' : '编辑';
        
        $result && action_log($handle_text, '广告' . $handle_text . '，title：' . $data['title']);
        
        return $result ? [RESULT_SUCCESS, '广告操作成功', $url] : [RESULT_ERROR, $this->modelAd->getError()];
    }

    /**
     * 获取广告信息
     */
    public function getAdInfo($where = [], $field = 'a.*,c.title as category_name')
    {
        
        $this->modelAd->alias('a');
        
        $join = [
					[SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
                    [SYS_DB_PREFIX . 'ad_cat c', 'a.catid = c.id'],
                ];
        
        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];
        
        $this->modelAd->join = $join;
        
        return $this->modelAd->getInfo($where, $field);
    }
    
    /**
     * 获取分类信息
     */
    public function getAdCatInfo($where = [], $field = true)
    {
        
        return $this->modelAdCat->getInfo($where, $field);
    }
    
    /**
     * 获取广告分类列表
     */
    public function getAdCatList($where = [], $field = true, $order = '', $paginate = 0)
    {
        
        return $this->modelAdCat->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 广告分类删除
     */
    public function adCategoryDel($where = [])
    {
        
        $result = db('ad_cat')->where($where)->delete();
        
        $result && action_log('删除', '广告分类删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '广告分类删除成功'] : [RESULT_ERROR, $this->modelAdCat->getError()];
    }
    
    /**
     * 广告删除
     */
    public function adDel($where = [])
    {
        
        $result = $this->modelAd->deleteInfo($where);
        
        $result && action_log('删除', '广告删除，where：' . http_build_query($where));
        
        return $result ? [RESULT_SUCCESS, '广告删除成功'] : [RESULT_ERROR, $this->modelAd->getError()];
    }
}
